package com.joshua.spring.exception;

import com.joshua.spring.entity.em.ResponseEnum;
import com.joshua.spring.entity.vo.GlobalResponse;
import com.joshua.spring.util.JacksonUtils;
import lombok.Data;

/**
 * description
 * package com.joshua.spring.exception
 * classname  MicroServiceException
 *
 * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
 * @date 2020/3/30 16:39
 */
@Data
public class MicroServiceException extends RuntimeException {

    private GlobalResponse globalResponse;

    MicroServiceException(GlobalResponse globalResponse) {
        this.globalResponse = globalResponse;
    }

    private MicroServiceException() {
        this.globalResponse = GlobalResponse.buildFail();
    }

    private MicroServiceException(String msg) {
        this.globalResponse = GlobalResponse.buildFail(msg);
    }

    private MicroServiceException(ExceptionEnumInter responseEnum) {
        this.globalResponse = GlobalResponse.buildFail(responseEnum);
    }

    private MicroServiceException(ExceptionEnumInter responseEnum, String... msg) {
        this.globalResponse = GlobalResponse.buildFail(responseEnum, msg);
    }

    @Override
    public String getMessage() {
        return JacksonUtils.obj2json(globalResponse);
    }

    @Override
    public String toString() {
        GlobalResponse globalResponse = this.globalResponse;
        return globalResponse.toString();
    }

    public static MicroServiceException builder(ResponseEnum responseEnum) {
        return new MicroServiceException(responseEnum);
    }

    public static MicroServiceException builder(ExceptionEnumInter responseEnum, String... msg) {
        return new MicroServiceException(responseEnum, msg);
    }

    public static MicroServiceException builder(String msg) {
        return new MicroServiceException(msg);
    }

    public static MicroServiceException builder() {
        return new MicroServiceException();
    }

}


